/**
 * Created by 占欧弟 on 2016/12/24.
 */
$(function(){

    itcast.iScroll({
        swipeDom:document.querySelector(".tab"),
        swipeType:"x",
        swipeDistance:40
    });
})


function news(){
    this.init()
}
news.prototype = {
    constructor:news,
    init:function(){
        this.tabIncident();
        this.getContent();
        this.shiftHome();
        this.search();
    },
    tabIncident:function(){
        var lis = $(".tab ul>li");
        var ulWidth = lis.length * 7.4+"rem";
        $(".tab ul").get(0).style.width = ulWidth;
        lis.on("tap",function(){
            $(this).addClass("current").siblings().removeClass("current");
        })

    },
    getContent:function(){
        var contentH = document.querySelector(".content")
        function setHeight(){
            var headerH = $(".header").height();
            var tabH = $(".tab").height();
            var footH = $(".footer").height();
            var marginT = headerH + tabH;
            contentH.style.height = window.screen.availHeight-marginT-footH+"px";
        }
        setHeight();


        var contentLinener = document.getElementById("content-ul");

        function fn(callback){
            $.ajax({
                type:"get",
                url:"js/data.json",
                success:function(result){
                    var cont = template("content",{"data":result})
                    contentLinener.innerHTML += cont;
                    var liNum =  $(".content-c").length;
                    var liH = $(".content-c").height();
                    var contentLinenerH = $(contentLinener).height(liNum * liH);
                    callback(contentLinenerH.height(),liH);
                    $(window).on("resize",function(){
                        liH = $(".content-c").height();
                        console.log(liH)
                        contentLinenerH = $(contentLinener).height(liNum * liH);
                        setHeight();
                    })
                }
            })
        }
        fn(setContent);
        function setContent(contentLinenerH,liH){
            var minpositionY = contentH.offsetHeight - contentLinenerH;
            var maxpositionY = 0;
            var startY = 0;
            var moveY = 0;
            var gap = 0;
            var currentY = 0
            contentLinener.addEventListener("touchstart",function(e){
                startY = e.touches[0].clientY;
            })
            contentLinener.addEventListener("touchmove",function(e){
                console.log(liH)
                moveY = e.touches[0].clientY;
                gap = moveY - startY;
                if(gap+currentY > maxpositionY+liH || gap+currentY < minpositionY - liH){
                    return;
                }
                contentLinener.style.transition = "none"
                contentLinener.style.webkitTransition = "none"
                contentLinener.style.transform = "translateY("+(gap+currentY)+"px)";
                contentLinener.style.webkitTransition = "translateY("+(gap+currentY)+"px)";
            })
            contentLinener.addEventListener("touchend",function(){
                if(gap+currentY > maxpositionY){
                    currentY = maxpositionY;
                    contentLinener.style.transition = "transform .4s"
                    contentLinener.style.webkitTransition = "transform .4s"
                    contentLinener.style.transform = "translateY("+maxpositionY+"px)";
                    contentLinener.style.webkitTransition = "translateY("+(gap+currentY)+"px)";
                }else if(gap+currentY < minpositionY){
                    currentY = minpositionY;
                    contentLinener.style.transition = "transform .4s"
                    contentLinener.style.webkitTransition = "transform .4s"
                    contentLinener.style.transform = "translateY("+minpositionY+"px)"
                    contentLinener.style.webkitTransition = "translateY("+(gap+currentY)+"px)";
                }else{
                    currentY += gap;
                }
            })
        }
    },
    shiftHome:function(){
        var home = $(".home-box");

        var target = 20;
        var index = 0;
        $(".side").on("tap.show",function(){
            if(index % 2 ==0){
                target = 62;
            }else{
                target = 0;
            }
            console.log(index)
            console.log(target)
            home.css({
                transition:"all .3s",
                transform:"translateX("+target+"%)"
            })
            index++;
        })
    },
    search:function(){
        $(".search").on("tap",function(){
            console.log(123)
            $(".search-box").css({
                transition:"transform .4s",
                transform:"translateX("+(-100)+"%)"
            })
            $(".cancel").on("tap",function(){
                $(".search-box").css({

                    transform:"translateX("+(100)+"%)"
                })
            })
        })
    }
}
